<?php
$myweburl="../";
if($sj1==""){
	$myweburl="";
	$shangci = GETTIME;
	$sj1= date("Ymd");
}else{
	$shangci=time()-1000;
}
$TaobaokeItem1='';
$TaobaokeItem='';
if(time() - $shangci > 600){
	$Taoapi_Config = Taoapi_Config::Init();
	$Taoapi_Config->setCharset('UTF-8');		        
	$Taoapi = new Taoapi;

	$tip=1;
	while($tip>0){
		$sj= date("Y-m-d H:i:s");
		$Taoapi->method = 'taobao.taobaoke.report.get';
		$Taoapi->fields = 'app_key,outer_code,trade_id,pay_time,pay_price,num_iid,item_title,item_num,category_id,category_name,shop_title,commission_rate,commission,iid,seller_nick';
		$Taoapi->date = $sj1;
		$Taoapi->page_no = $tip;
		$Taoapi->page_size=40;
		$TaobaokeData = $Taoapi->Send('get','xml')->getArrayData();		
		$TaobaokeItem1 = $TaobaokeData['taobaoke_report']['taobaoke_report_members']['taobaoke_report_member']; 
		$total_results=$TaobaokeData['taobaoke_report']['total_results'];

		$page=ceil($total_results/40);
		if($page>1){
			if($tip==1){
				$tip=$page;
			}else{
				if($tip==2){
					$tip=0;
				}else{
					$tip--;
				}
			}
		}else{
			$tip=0;
		}
	
		if($total_results==1){
			$TaobaokeItem[0]=$TaobaokeItem1;
		}else{
			$TaobaokeItem=$TaobaokeItem1;
		}

		for($i = 0; $i < count($TaobaokeItem); $i++){
			$mynum=0;
			$sql="select count(Id) as idnum from ".$BIAOTOU."tradelist where trade_id='".$TaobaokeItem[$i]["trade_id"]."'";
			$rs1=mysql_query($sql);
			while ($row1 = mysql_fetch_array($rs1)){
				$mynum = $row1["idnum"];
			}
			if($mynum==0){
				$snum=$snum+1;
				$commission=$TaobaokeItem[$i]['commission']; 
				$outer_code=$TaobaokeItem[$i]['outer_code'];
				$outer_code_length=strlen($outer_code);
				if($outer_code_length>=8){
				    $outer_code=substr($outer_code,8,10);
				}
				if($outer_code>0){
					$sql="select dengji from ".$BIAOTOU."user where Id='$outer_code'";
					$query=mysql_query($sql);
					$row=mysql_fetch_array($query);
					$fxje=ck_fxje($commission,$row['dengji']);
					$checked=2; 
				}elseif($outer_code==0){
					$fxje=ck_fxje($commission,0);
					$checked=0;  
					$outer_code='';
					$TaobaokeItem[$i]['outer_code']='';
				}
				$trade_id=$TaobaokeItem[$i]['trade_id']; 
				$item_title=str_replace("'","",$TaobaokeItem[$i]['item_title']);
				$shop_title=str_replace("'","",$TaobaokeItem[$i]['shop_title']);
				$sql = "INSERT into ".$BIAOTOU."tradelist(item_title,shop_title,pay_price,commission_rate,commission,item_num,trade_id,pay_time,num_iid,outer_code,seller_nick,fxje,checked) VALUES('".$item_title."','".$shop_title."','".$TaobaokeItem[$i]['pay_price']."','".$TaobaokeItem[$i]['commission_rate']."','".$TaobaokeItem[$i]['commission']."','".$TaobaokeItem[$i]['item_num']."','".$TaobaokeItem[$i]['trade_id']."','".$TaobaokeItem[$i]['pay_time']."','".$TaobaokeItem[$i]['num_iid']."','".$TaobaokeItem[$i]['outer_code']."','".$TaobaokeItem[$i]['seller_nick']."','$fxje','$checked')"; 
				$allow_insert=mysql_query($sql);
				
				//有会员标记的情况	
				if($outer_code>0&& $allow_insert){
					$sql="select ddusername,dengji from ".$BIAOTOU."user where Id=".$outer_code."";
					$rs=mysql_query($sql);
					//该代号的会员如果存在
					if(mysql_num_rows($rs)!=0)
					{
						while($rowuser = mysql_fetch_object($rs))
						{
							$ddusername=$rowuser->ddusername; 
							$dddengji=$rowuser->dengji; 
						}
						//用户佣金
						$fxje=ck_fxje($commission,$dddengji);
						$sql="update ".$BIAOTOU."user set money=money+".$fxje.",dengji=dengji+1 where ddusername ='".$ddusername."'";
						mysql_query($sql);
						//写入返现金额到交易列表
						$sql="update ".$BIAOTOU."tradelist set fxje=".$fxje.",qrsj='".$sj."' where trade_id ='".$trade_id."'";
						mysql_query($sql);
						//用户消息
						$msg_tabao=str_replace("[trade_id]",$trade_id,$msg_tabao);
						$msg_tabao=str_replace("[fxje]",$fxje,$msg_tabao);
						$sql="insert into ".$BIAOTOU."msg (title,content,addtime,see,ddusername,senduser) values ('您获得了新的交易返现！','".$msg_tabao."','$sj',0,'".$ddusername."','网站客服');";
						mysql_query($sql);
						$msg_tabao=str_replace($trade_id,"[trade_id]",$msg_tabao);
						$msg_tabao=str_replace($fxje,"[fxje]",$msg_tabao);
						//用户明细
						$sql="insert into ".$BIAOTOU."mingxi (ddusername,shijian,je,addtime,memo) values ('".$ddusername."','交易返现',".$fxje.",'$sj','交易号".$trade_id."')";
						mysql_query($sql);							
						//取推荐人
						$sql="select tjr from ".$BIAOTOU."user where ddusername='".$ddusername."' and tjr is not NULL and tjr<>''";
						$t=mysql_query($sql);
						$num1 = mysql_num_rows($t);	
						//如果推荐人存在，就进行下面的操作					
						if($num1!=0){   
							$tjrid="";
							while ($tt = mysql_fetch_array($t)){
								$tjrid=$tt["tjr"]; //推广人佣金
								$sql="update ".$BIAOTOU."user set money=money+".round($commission*TGBL,2)." where Id='".$tjrid."'";
								mysql_query($sql);
								
								//取推荐人名
								$sqltjr="select ddusername from ".$BIAOTOU."user where Id='".$tjrid."'";
								$tgrq=mysql_query($sqltjr);
								while ($tu = mysql_fetch_array($tgrq)){
									$tjrusername=$tu["ddusername"]; 
								}	
								if($tjrusername!=''){
								    //推广人明细
								    $sql="insert into ".$BIAOTOU."mingxi (ddusername,shijian,je,addtime,memo) values ('".$tjrusername."','推广佣金',".round($commission*TGBL,2).",'$sj','交易人".$ddusername."，交易号".$trade_id."')";
								    mysql_query($sql);
								//推广人消息
								    $tgje=round($commission*TGBL,2);
									$msg_taobaotuiguang_temp=$msg_taobaotuiguang;
								    $msg_taobaotuiguang_temp=str_replace("[ddusername]",$ddusername,$msg_taobaotuiguang_temp);
								    $msg_taobaotuiguang_temp=str_replace("[trade_id]",$trade_id,$msg_taobaotuiguang_temp);
								    $msg_taobaotuiguang_temp=str_replace("[tgje]",$tgje,$msg_taobaotuiguang_temp);
								    $sql="insert into ".$BIAOTOU."msg (title,content,addtime,see,ddusername,senduser) values ('您获得了新的推广佣金！','".$msg_taobaotuiguang_temp."','$sj',0,'".$tjrusername."','网站客服');";
								    mysql_query($sql);
								}
							}
						}
					}					
				}					
			}else{
				$fnum=$fnum+1;
			}
		}
	}
	$sql="update ".$BIAOTOU."websetting set gettime='".time()."'";
	mysql_query($sql);
}
?>